package com.twotiger.and.activity.project;

import java.util.HashMap;

import android.content.Intent;
import android.net.Uri;
import android.os.Message;
import android.view.LayoutInflater;
import android.view.View;
import android.widget.Button;
import android.widget.TextView;
import app.twotiger.p2p.R;

import com.alibaba.fastjson.JSONObject;
import com.twotiger.and.Constants;
import com.twotiger.and.activity.HomeActivity;
import com.twotiger.and.activity.base.BaseActivity;
import com.twotiger.and.activity.base.BaseHandler;
import com.twotiger.and.activity.base.SubTitle;
import com.twotiger.and.bean.ConstantDataUnit;
import com.twotiger.and.bean.Contact;
import com.twotiger.and.util.EaseHXUtils;
import com.twotiger.and.util.IntentUtil;
import com.twotiger.and.util.PromptManager;
import com.twotiger.and.util.PromptManager.ClickListener;
import com.twotiger.and.util.TimeUtils;

/**
 * 
 * @Description: 
 * 直投失败、转让失败页面
 * @Author douxy
 * @Date 2015年8月12日 下午1:34:44
 */
public class InvestFailurePage extends BaseActivity {

    public static final String TYPE_TENDER = "investtender";//业务流程---直投项目
    public static final String TYPE_DEBT = "investdebt";//业务流程---转让项目投资

    public static final String TAG_CLICK_TELE = "telephone";
    public static final String TAG_CLICK_QQ = "qq";

    private static final int GETCONTACTSUCCESS = 0;
    private static final int GETCONTACTFAILURE = 1;

    private static HashMap<String, String> params;
    private SubTitle subTitle;
    private TextView tv_above;
    private TextView tv_below;
    private Button btn_right;
    private TextView tv_tele;
    private TextView tv_qq;
    private TextView tv_assert;

    private String title;
    private String type;
    private String msg;

    private String tag_click; //区分用户是电话联系还是qq联系

    @Override
    protected void processLogic() {
        params = getParm();

        title = getIntent().getStringExtra("TITLE");
        type = getIntent().getStringExtra("TYPE");
        msg = getIntent().getStringExtra("MSG");

        initPage();
    }

    private void initPage() {
        subTitle.setTitle(title);
        tv_above.setText(title + "");
        tv_below.setText("失败原因：" + msg);
    }

    @Override
    protected void setListener() {
        handler = new BaseHandler(this) {

            @Override
            public void handleMessage(Message msg) {
                switch (msg.what) {
                case GETCONTACTSUCCESS:
                    Contact mContact = JSONObject.parseObject((String) msg.obj, Contact.class);
                    if (mContact.isOk()) {
                        if (TAG_CLICK_TELE.equals(tag_click)) {
                            String telNum = mContact.getTelNum();
                            IntentUtil.CallPhone(InvestFailurePage.this, telNum);
                        } else if (TAG_CLICK_QQ.equals(tag_click)) {
                            String qqServUrl = mContact.getQqServUrl();
                            String qqType = mContact.getQqType();

                            if ("personal".equals(qqType)) { //需根据时间弹窗
                                String timeNow = TimeUtils.getFormatTime1();
                                String qqServTime = mContact.getQqServTime();
                                String[] timeArray = qqServTime.split("-");
                                int nowTimeToMinute = TimeUtils.timeToMinute(timeNow);
                                int qqServTimePreToMinute = TimeUtils.timeToMinute(timeArray[0]);
                                int qqServTimeEndToMinute = TimeUtils.timeToMinute(timeArray[1]);
                                if (nowTimeToMinute < qqServTimePreToMinute
                                    || nowTimeToMinute > qqServTimeEndToMinute) {
                                    PromptManager.showContactAlert(InvestFailurePage.this,
                                        "工作时间：" + qqServTime + "\n请在工作时间联系哦", "我知道了", "",
                                        new ClickListener() {

                                            @Override
                                            public void onClickRight() {
                                            }

                                            @Override
                                            public void onClickLeft() {
                                            }
                                        }, false);
                                } else {
                                    Intent intent = new Intent(Intent.ACTION_VIEW,
                                        Uri.parse(qqServUrl));
                                    if (IntentUtil.queryIntentActivities(InvestFailurePage.this,
                                        intent)) {
                                        startNewActivity(intent, R.anim.push_left_in,
                                            R.anim.push_left_out, false);
                                    } else {
                                        showToast("您的手机还未安装QQ,请先下载QQ");
                                    }
                                }
                            } else { //直接进入聊天，无需再根据时间判断是否弹窗
                                Intent intent = new Intent(Intent.ACTION_VIEW, Uri.parse(qqServUrl));
                                if (IntentUtil
                                    .queryIntentActivities(InvestFailurePage.this, intent)) {
                                    startNewActivity(intent, R.anim.push_left_in,
                                        R.anim.push_left_out, false);
                                } else {
                                    showToast("您的手机还未安装QQ,请先下载QQ");
                                }
                            }
                        }
                    } else {
                        showToast(mContact.codeDesc);
                    }
                    break;
                case GETCONTACTFAILURE:
                    showToast("网络异常");
                    break;
                default:
                    break;
                }

                super.handleMessage(msg);
            }
        };

        btn_right.setOnClickListener(this);
        tv_tele.setOnClickListener(this);
        tv_qq.setOnClickListener(this);
        tv_assert.setOnClickListener(this);
    }

    @Override
    protected void findViewById(View view) {
        subTitle = new SubTitle(view) {
            @Override
            protected void clickTitle(View v) {
            }

            @Override
            protected void clickRightBtn(View v) {
            }

            @Override
            protected void cickLeftBtn(View v) {
                onBackPress();
            }
        };
        subTitle.setTitle("总资产");
        subTitle.hideBtns();
        subTitle.title_left_bt.setVisibility(View.VISIBLE);

        tv_above = (TextView) view.findViewById(R.id.tv_above);
        tv_below = (TextView) view.findViewById(R.id.tv_below);
        btn_right = (Button) view.findViewById(R.id.btn_right);
        tv_tele = (TextView) view.findViewById(R.id.tv_tele);
        tv_qq = (TextView) view.findViewById(R.id.tv_qq);
        tv_assert = (TextView) view.findViewById(R.id.tv_assert);

    }

    @Override
    protected View loadViewLayout(LayoutInflater inflater) {
        View view = inflater.inflate(R.layout.invest_failure_layout, null);
        return view;
    }

    @Override
    public void onClick(View view) {
        switch (view.getId()) {
        case R.id.btn_right:
            setResult(ProjectDetailPage.TENDERRESULTCLOSE_FAI);
            finish();
            break;
        case R.id.tv_assert:
            startNewActivity(new Intent(InvestFailurePage.this, HomeActivity.class).putExtra(
                "BUTTON", R.id.radio_btn4), R.anim.push_left_in, R.anim.push_left_out, true);
            break;
        case R.id.tv_qq:
            tag_click = TAG_CLICK_QQ;
            //            getContact();
            new EaseHXUtils().startEaseChat(mContext);
            break;
        case R.id.tv_tele:
            tag_click = TAG_CLICK_TELE;
            getContact();
            break;
        default:
            break;

        }
    }

    /**
     * 获取客服电话和客服qq
     */
    protected void getContact() {
        //        params.clear();
        //        getDate(params, Constants.constant_query, handler, GETCONTACTSUCCESS, GETCONTACTFAILURE, true, false,
        //            false);
        if (TAG_CLICK_TELE.equals(tag_click)) {
            ConstantDataUnit cl_kfdh = getConstantDataByKey(Constants.cl_kfdh);
            if (cl_kfdh != null) {
                String telNum = cl_kfdh.getVal();
                IntentUtil.CallPhone(InvestFailurePage.this, telNum);
            }
        } else if (TAG_CLICK_QQ.equals(tag_click)) {

        }
    }

    @Override
    protected void onBackPress() {
        setResult(ProjectDetailPage.TENDERRESULTCLOSE_FAI);
        finish();
        overridePendingTransition(R.anim.push_right_in, R.anim.push_right_out);
    }

}
